/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	liuyang 
 * 
 * <p>描   述  :   SortList.java for com.pengtu.utils    </p>
 * 
 * <p>最后修改 : $: 2011-11-18-上午9:33:03 v 1.0.0	 liuyang   $     </p>
 * 
 */

package com.pengtu.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 
 * SortList
 * 
 * 2011-11-18 上午9:33:03
 * 
 * @version 1.0.0
 * 
 */
public class SortList<E> {
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void Sort(List<E> list, final String method, final String sort) {
		Collections.sort(list, new Comparator() {
			@Override
			public int compare(Object a, Object b) {
				// TODO Auto-generated method stub
				int ret = 0;
				try {
					Method m1 = ((E) a).getClass().getMethod(method, null);
					Method m2 = ((E) b).getClass().getMethod(method, null);
					if (sort != null && "desc".equals(sort)) {
						ret = m2.invoke(((E) b), null).toString()
								.compareTo(m1.invoke(((E) a), null).toString());
					} else {
						ret = m1.invoke(((E) a), null).toString()
								.compareTo(m2.invoke(((E) b), null).toString());
					}
				} catch (NoSuchMethodException ne) {
					// TODO Auto-generated catch block
					System.out.println(ne);
				} catch (IllegalAccessException ie) {
					// TODO Auto-generated catch block
					System.out.println(ie);
				} catch (InvocationTargetException it) {
					// TODO Auto-generated catch block
					System.out.println(it);
				}
				return ret;
			}

		});
	}
}
